package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.alone.AloneTyreBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.List;

public class X3HomeTyreSizeSelectDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {


    private TextView tv_title;
    private TextView tv_cancel;
    private TextView tv_affirm;
    private WheelView<String> wv_one;
    private WheelView<String> wv_two;
    private WheelView<String> wv_three;
    private LinearLayout ll_select;
    private ImageView iv_select;
    private String sn;

    private boolean isSelect;
    private String answer;

    public X3HomeTyreSizeSelectDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_title = findViewById(R.id.tv_title);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_affirm = findViewById(R.id.tv_affirm);
        wv_one = findViewById(R.id.wv_one);
        wv_two = findViewById(R.id.wv_two);
        wv_three = findViewById(R.id.wv_three);
        ll_select = findViewById(R.id.ll_select);
        iv_select = findViewById(R.id.iv_select);

        tv_cancel.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);
        ll_select.setOnClickListener(this);


        wv_one.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_one.setSoundEffectResource(R.raw.x3_button_choose);
        wv_one.setSoundEffect(true);
        wv_one.setPlayVolume(0.5f);

        wv_two.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_two.setSoundEffectResource(R.raw.x3_button_choose);
        wv_two.setSoundEffect(true);
        wv_two.setPlayVolume(0.5f);

        wv_three.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_three.setSoundEffectResource(R.raw.x3_button_choose);
        wv_three.setSoundEffect(true);
        wv_three.setPlayVolume(0.5f);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_home_tyre_size_number_picker;
    }

    @Override
    public void show() {
        super.show();
        requestTyreSize();
    }

    /**
     * 请求所有轮胎参数
     */
    private void requestTyreSize() {
        NetWorkHttp.getApi().obtainAllTyreParameter()
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<AloneTyreBean>>() {
                    @Override
                    protected void onDone(List<AloneTyreBean> aloneTyreBeans) {
                        if (X3StringUtils.isListEmpty(aloneTyreBeans)) {
                            return;
                        }
                        showTyreSize(aloneTyreBeans);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    private void showTyreSize(List<AloneTyreBean> aloneTyreBeans) {
        List<String> tyreWidth = new ArrayList<>();
        List<String> diameter = new ArrayList<>();
        List<String> flatRatio = new ArrayList<>();
        for (int i = 0; i < aloneTyreBeans.size(); i++) {
            String tyreWidth1 = aloneTyreBeans.get(i).getTyreWidth();
            String diameter1 = aloneTyreBeans.get(i).getDiameter();
            String flatRatio1 = aloneTyreBeans.get(i).getFlatRatio();
            if (!X3StringUtils.isEmpty(tyreWidth1)) {
                tyreWidth.add(tyreWidth1);
            }
            if (!X3StringUtils.isEmpty(diameter1)) {
                diameter.add(diameter1);
            }
            if (!X3StringUtils.isEmpty(flatRatio1)) {
                flatRatio.add(flatRatio1);
            }
        }

        wv_one.setData(tyreWidth);
        wv_two.setData(flatRatio);
        wv_three.setData(diameter);

        if (X3StringUtils.isEmpty(answer)) {
            return;
        }
        String[] split = answer.split("-");
        if (split == null) {
            return;
        }
        if (split.length < 1) {
            return;
        }
        if (tyreWidth.contains(split[0])) {
            wv_one.setSelectedItemPosition(tyreWidth.indexOf(split[0]));
        }
        if (split.length < 2) {
            return;
        }
        if (diameter.contains(split[1])) {
            wv_two.setSelectedItemPosition(diameter.indexOf(split[1]));
        }

        if (split.length < 3) {
            return;
        }
        if (flatRatio.contains(split[2])) {
            wv_three.setSelectedItemPosition(flatRatio.indexOf(split[2]));
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_affirm) {
            dismiss();
            if (iOnClick != null) {
                iOnClick.iOnClickTyreSelect(wv_one.getSelectedItemData() + "-" + wv_two.getSelectedItemData() + "-" + wv_three.getSelectedItemData(), isSelect, sn);
            }
        } else if (id == R.id.ll_select) {
            if (isSelect) {
                iv_select.setImageResource(R.mipmap.x3_car_no_select);
            } else {
                iv_select.setImageResource(R.mipmap.x3_car_select);
            }
            isSelect = !isSelect;
        }
    }

    IOnClickTyreCar iOnClick;

    public void setiOnClick(IOnClickTyreCar iOnClick) {
        this.iOnClick = iOnClick;
    }

    public void setShowSn(String sn,String answer) {
        this.sn = sn;
        this.answer = answer;
    }

    public interface IOnClickTyreCar {
        void iOnClickTyreSelect(String tyreSize,boolean isAll,String sn);
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息选择轮胎尺寸弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("初检信息选择轮胎尺寸弹窗");
    }
}
